package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayActivity;
import org.itboys.mobile.service.mongo.web.SundayActivityService;
import org.itboys.mobile.util.ResponseMessage;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author：贾琦磊
 * @Date：2018/7/10
 * @Description：web端活动管理接口
 */
@Controller
@RequestMapping("/sunday/web/activity")
public class SundayActivityController extends BaseController{
    @Autowired
    private SundayActivityService activityService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request, HttpServletResponse response){
        return "sunday/activity/index";
    }

    @RequestMapping("/input")
    public String input(HttpServletRequest request, HttpServletResponse response,
                        @RequestParam("id")Long id,
                        Model model){
        try {
            model.addAttribute("activity",activityService.findOne(id));
           return "sunday/activity/input";
        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/activity/input------" + e.getMessage());
            }
            e.printStackTrace();

            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
        return "sunday/activity/error";
    }
    @RequestMapping("/select")
    public void select(HttpServletRequest request,HttpServletResponse response){
        try {
            PageResult<SundayActivity> result=activityService.select(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    @RequestMapping("/save")
    public void save(HttpServletRequest request, HttpServletResponse response
                    , @RequestParam("id")Long id, @RequestParam("sDate")String sDate, @RequestParam("eDate")String eDate
                    , @RequestParam("name")String name
            //, @RequestParam("recommend")String recommend
                    , @RequestParam("desc")String desc
                     ,@RequestParam("slogan")String slogan
                    , @RequestParam(value = "imageFile",required = false)MultipartFile imageFile
             , @RequestParam(value = "backImageFile",required = false)MultipartFile backImageFile
            , @RequestParam(value = "headImageFile",required = false)MultipartFile headImageFile){
        try {
            SundayActivity activity=new SundayActivity( name, slogan, sDate, eDate, desc);
            activityService.save(id,activity,imageFile,backImageFile,headImageFile);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,"error");
        }
    }

    /**
     * 删除活动
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,HttpServletResponse response
    ,@RequestParam("id") Long id){
        try {
           activityService.delete(id);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,"error");
        }
    }

    /**
     * 修改活动状态
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/change")
    public void change(HttpServletRequest request,HttpServletResponse response
    ,@RequestParam("id")Long id
    ,@RequestParam("status")Integer status){
        try {
            activityService.change(id,status);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,"error");
        }
    }
    /**
     * 修改上下架
     * @param request
     * @param response
     * @param id
     */
    @RequestMapping("/setRecommend")
    public void setRecommend(HttpServletRequest request,HttpServletResponse response
            ,@RequestParam("id")Long id
            ,@RequestParam("status")Integer status){
        try {
            activityService.setRecommend(id,status);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,"error");
        }
    }

    @RequestMapping("/choose" )
    public  String choose(HttpServletRequest request,
                          @RequestParam(value="isSingle",required=true)Integer isSingle,//是否单选
                          @RequestParam(value="callBack",required=false)String callBack,//回调参数
                          @RequestParam(value="param",required=false)String param,//查询参数
                          Model model){
        model.addAttribute("param",param);
        model.addAttribute("callBack",callBack);
        if(isSingle==null){
            return "/sunday/activity/choose/choose_erro";
        }
        return "/sunday/activity/choose/choose_single";
    }
}
